package bg.cinema_system.beans;

import java.sql.SQLException;
import java.util.Date;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import bg.cinema_system.service.MovieService;

@ManagedBean(name = "movie")
@RequestScoped
public class MovieBean {

	private long movie_id;
	private String name;
	private String movie_type;
	private Date duration;
	private String director;
	private String actors;
	private String country;
	private String summary;

	public long getMovie_id() {
		return movie_id;
	}

	public void setMovie_id(long movie_id) {
		this.movie_id = movie_id;
	}

	public String getName() throws SQLException {
		setMovie_id();
		setMovieObject();

		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getMovie_type() {
		return movie_type;
	}

	public void setMovie_type(String movie_type) {
		this.movie_type = movie_type;
	}

	public Date getDuration() {
		return duration;
	}

	public void setDuration(Date duration) {
		this.duration = duration;
	}

	public String getDirector() {
		return director;
	}

	public void setDirector(String director) {
		this.director = director;
	}

	public String getActors() {
		return actors;
	}

	public void setActors(String actors) {
		this.actors = actors;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getSummary() {
		return summary;
	}

	public void setSummary(String summary) {
		this.summary = summary;
	}

	// Set the "movie id", using the passed parameter
	public void setMovie_id() {
		String movie_id = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap().get("movie_id");

		this.movie_id = Long.parseLong(movie_id);
	}

	// Set all fields of the MovieBean, using the JDBC connection to the
	// database using the "movie id"
	public void setMovieObject() throws SQLException {
		MovieBean movie = new MovieBean();
		movie = MovieService.getMovie(this.movie_id);

		this.name = movie.name;
		this.movie_type = movie.movie_type;
		this.duration = movie.duration;
		this.director = movie.director;
		this.actors = movie.actors;
		this.country = movie.country;
		this.summary = movie.summary;
	}
}
